﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace FastInsertTableControl
{
    public partial class FastInsertTable : UserControl
    {
        public event FastInsertTableEvents.SelectCompleteEventHandler SelectComplete;
        public event FastInsertTableEvents.CancelSelectEventHandler CancelSelect;
        public event FastInsertTableEvents.SelectingEventHandler Selecting;

        public FastInsertTable()
        {
            InitializeComponent();
            for (int i = 0; i < 10; i++)
            {
                dataGridView1.Rows.Add();
                for (int j = 0; j < 10; j++)
                    dataGridView1[j, i].Value = "";
            }
            dataGridView1.ClearSelection();
        }

        private void dataGridView1_MouseLeave(object sender, EventArgs e)
        {
            dataGridView1.ClearSelection();
            if (CancelSelect != null)
            {
                CancelSelect(this, e);
            }
        }

        private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                for (int j = 0; j < dataGridView1.ColumnCount; j++)
                {
                    dataGridView1[j, i].Selected = (i <= e.RowIndex && j <= e.ColumnIndex);
                }
            }
            if (Selecting != null)
            {
                FastInsertTableEvents.SelectingEventArgs args = new FastInsertTableEvents.SelectingEventArgs(e.RowIndex + 1, e.ColumnIndex + 1);
                Selecting(this, args);
            }
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                for (int j = 0; j < dataGridView1.ColumnCount; j++)
                {
                    dataGridView1[j, i].Selected = (i <= e.RowIndex && j <= e.ColumnIndex);
                }
            }
        }

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (SelectComplete != null)
            {
                FastInsertTableEvents.SelectCompleteEventArgs args = new FastInsertTableEvents.SelectCompleteEventArgs(e.RowIndex + 1, e.ColumnIndex + 1);
                SelectComplete(this, args);
            }
        }
    }
}
